﻿using Dapper;
using DapperExtensions;
using KD.Service.HR.Utility;
using Logistics.Model;
using Logistics.Model.QueryModel;
using Logistics.Model.Response;
using Logistics.Service;
using Logistics.Utility;
using Logistics.WebApi.Attributes;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Web;
using System.Web.Http;

namespace Logistics.WebApi.Controllers
{
   [MyAuthority]
    public class BidBaseController : ApiController
    {
        private BidBaseService bidService = new BidBaseService();
        private AccountService accountService = new AccountService();
        private CompanyService companyService = new CompanyService();
        private BidLogService logService = new BidLogService();
        private BidLoadService bidLoadService = new BidLoadService();
        //private AndroidTokenService androidService = new AndroidTokenService();

         /// <summary>
        /// 手机端调用
        /// </summary>
        /// <param name="bidbase"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetPhoneBidInfo(BidPhoneQuery bidbase)
        {
            ResponseModel resultmod = new ResponseModel();
            if (bidbase.flag == 1 || bidbase.flag == 2 || bidbase.flag == 3)
            {
                if(bidbase.bid_id!=null)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                    resultmod.mod = bidService.GetPhoneBidInfo(bidbase.bid_id);
                }
            }
            if (bidbase.flag == 4 || bidbase.flag == 5 || bidbase.flag == 6)
            {
                if (bidbase.load_id>0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                    resultmod.mod = bidLoadService.GetPhoneLoadInfo(bidbase.flag, bidbase.load_id);
                }
                else
                {
                    resultmod.resmessage = "参数：load_id的运单ID不能为空";
                    resultmod.recode = "000000";
                }
               
            }
            return resultmod;
        }

        /// <summary>
        /// 手机端调用
        /// </summary>
        /// <param name="bidbase"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetBidInfo(BidPhoneQuery bidbase)
        {
            ResponseModel resultmod = new ResponseModel();

            if (bidbase.bid_id != null)
            {
                resultmod.InitResultState(GetDataStateEnum.Success);
                resultmod.mod = bidService.GetPhoneBidInfo(bidbase.bid_id);
            }
            else
            {
                resultmod.InitResultState(GetDataStateEnum.Error);
            }

            return resultmod;
        }


        /// <summary>
        /// 手机端调用
        /// </summary>
        /// <param name="bidbase"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetPhoneBidList(BidPhoneQuery bidbase)
        {
            ResponseModel resultmod = new ResponseModel(); ResponseModel bidmod = new ResponseModel();
            if(bidbase.flag==1||bidbase.flag==2||bidbase.flag==3)
            {
                BidQuery query = new BidQuery();
                if (bidbase.page != null)
                {
                    if (bidbase.page > 0)
                    {
                        query.page = bidbase.page;
                    }
                }
                if (bidbase.pagesize != null)
                {
                    if (bidbase.pagesize > 0)
                    {
                        query.pagesize = bidbase.pagesize;
                    }
                }
                query.del_flag = 1;
                if (bidbase.flag == 1)//未报价
                {
                    query.state = 1;
                }
                if (bidbase.flag == 2)//竟标中
                {
                    query.state = 10;
                }
                if (bidbase.flag == 3)//选标中
                {
                    query.state = 20;
                }
                bidmod = GetBidListPhone(query);

            }
            if(bidbase.flag==4||bidbase.flag==5||bidbase.flag==6)
            {
                BidLoadQuery query = new BidLoadQuery();
                if (bidbase.page != null)
                {
                    if (bidbase.page > 0)
                    {
                        query.page = bidbase.page;
                    }
                }
                if (bidbase.pagesize != null)
                {
                    if (bidbase.pagesize > 0)
                    {
                        query.pagesize = bidbase.pagesize;
                    }
                }
                if (bidbase.flag == 4)//取货中
                {
                    query.load_state = 1;
                    
                }
                if (bidbase.flag == 5)//回单确认
                {
                    query.load_state = 200;
                }
                if(bidbase.flag==6)
                {
                    query.load_state = 40;
                }
                bidmod = GetPhoneBidLoad(query);
            }

            resultmod.InitResultState(GetDataStateEnum.Success);
            resultmod.mod = bidmod.mod;
            return resultmod;
        }

       /// <summary>
       /// 获取运单数据
       /// </summary>
       /// <param name="query"></param>
       /// <returns></returns>
        private ResponseModel GetPhoneBidLoad(BidLoadQuery query)
        {
            if (query == null)
                query = new BidLoadQuery();
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);
                var loadQuery = new List<IPredicate>();
                if (!string.IsNullOrEmpty(query.bid_no))
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.bid_no, Operator.Eq, query.bid_no));
                if (!string.IsNullOrEmpty(query.orderNumber))
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.order_num, Operator.Eq, query.orderNumber));
                if (query.load_state > 0)
                {
                    if (query.load_state == 200)
                    {
                        loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Ge, 20));
                        loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Le, 30));
                    }
                    else
                    {
                        if(query.load_state==1)
                        {
                            loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.bid_flag, Operator.Eq, "1"));
                            loadQuery.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "20"),
                                Predicates.Field<wuliu_bid_load>(f => f.wuliu_bid_state, Operator.Eq, "10")));
                            loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.company_no, Operator.Eq, company.company_no));
                            loadQuery.Add(Predicates.Group(GroupOperator.Or, Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Eq, "1"),
                                Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Eq, "10")));
                        }
                        else
                        {
                            loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Eq, query.load_state));
                            loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.bid_flag, Operator.Eq, 1));
                        }
                       
                    }
                }
                else
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.load_state, Operator.Gt, 30));

                if (query.consignee_time1 > 0)
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.consignee_time, Operator.Ge, query.consignee_time1 / 1000));
                if (query.consignee_time2 > 0)
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.consignee_time, Operator.Le, query.consignee_time2 / 1000));
                loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.company_no, Operator.Eq, company.company_no));

                if (query.pickup_time1 >0)
                {
                    DateTime pickuptime1 = DateConvert.StampToDateTime((query.pickup_time1 / 1000).ToString());
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.pickup_time, Operator.Ge,pickuptime1 ));
                }
                   
                if (query.pickup_time2 >0)
                {
                    DateTime pickuptime2 = DateConvert.StampToDateTime((query.pickup_time2 / 1000).ToString());
                    loadQuery.Add(Predicates.Field<wuliu_bid_load>(f => f.pickup_time, Operator.Le, pickuptime2));
                }
                var loadPredicate = Predicates.Group(GroupOperator.And, loadQuery.ToArray());
                var BidLoadList = bidLoadService.GetPhoneLoadList(loadPredicate, query.page - 1, query.pagesize);
                resultmod.mod = BidLoadList;
                if (BidLoadList.totalnum > 0)
                {
                    resultmod.InitResultState(GetDataStateEnum.Success);
                }
                else
                {
                    resultmod.InitResultState(GetDataStateEnum.Error01);
                }
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
                return resultmod;
        }

        private ResponseModel GetBidListPhone(BidQuery bidQuery)
        {
            var resultmod = new ResponseModel();

            var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            int company_id = companyService.GetCompanyIdByAccountId(accountId);
            var list = new List<IPredicate>();
            if (!string.IsNullOrEmpty(bidQuery.bid_no))
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.bid_no, Operator.Eq, bidQuery.bid_no));
            if (!string.IsNullOrEmpty(bidQuery.ordernumber))
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.number, Operator.Eq, bidQuery.ordernumber));
            if (bidQuery.state_bid_time1 > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.start_bid_time, Operator.Ge, bidQuery.state_bid_time1 / 1000));
            if (bidQuery.state_bid_time2 > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.start_bid_time, Operator.Le, bidQuery.state_bid_time2 / 1000));
            if (bidQuery.end_bid_time1 > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.end_bid_time, Operator.Gt, bidQuery.end_bid_time1 / 1000));
            if (bidQuery.end_bid_time2 > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.end_bid_time, Operator.Le, bidQuery.end_bid_time2 / 1000));


            list.Add(Predicates.Field<wuliu_bid_base>(f => f.company_id, Operator.Eq, company_id));

            if (bidQuery.del_flag > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.del_flag, Operator.Eq, bidQuery.del_flag));
            if (bidQuery.state > 0)
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Eq, bidQuery.state));
            else
            {
                if (bidQuery.del_flag > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Gt, 1));
                else
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Gt, 10));
            }


            if (!string.IsNullOrEmpty(bidQuery.wuliu_name))
                list.Add(Predicates.Field<wuliu_bid_base>(f => f.wuliu_name, Operator.Eq, bidQuery.wuliu_name));
            var pg = Predicates.Group(GroupOperator.And, list.ToArray());

            resultmod.InitResultState(GetDataStateEnum.Success);
            resultmod.mod = bidService.GetPhoneBidList(pg, bidQuery.page - 1, bidQuery.pagesize);
            return resultmod;
        }

       /// <summary>
       /// 查询标单页面
       /// </summary>
       /// <param name="bidbase"></param>
       /// <returns></returns>
        [HttpPost]
         public ResponseModel GetBidList(wuliu_bid_base_query bidbase)
        {
            var resultmod = new ResponseModel();
            try
            {
                //获取校验token的accountid
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                if (accountId != null)
                {
                    int company_id = companyService.GetCompanyIdByAccountId(accountId);
                    bidbase = bidbase ?? new wuliu_bid_base_query();
                    resultmod.mod = bidService.GetBidList(company_id);
                    resultmod.InitResultState(GetDataStateEnum.Success);

                }
                else
                {
                    resultmod.InitResultState(LoginEnum.Error04);
                }

            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        [HttpPost]
        public ResponseModel GetIndexBidCount()
        {
            var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            int company_id = companyService.GetCompanyIdByAccountId(accountId);
            var resultmod = new ResponseModel();
            var count1 = bidService.GetBidBaseListCount(company_id, 1);
            var count2 = bidService.GetBidBaseListCount(company_id,10);
            var count3 = bidService.GetBidBaseListCount(company_id, 20);
            var count4 = bidService.GetBidBaseListCount(company_id, 30);
            string strcount = count1 + "|" + count2+"|"+count3+"|"+count4;
            resultmod.mod = strcount;
            resultmod.InitResultState(GetDataStateEnum.Success);
            return resultmod;
        }

      /// <summary>
      /// 添加标单管理
      /// </summary>
      /// <param name="bidbase"></param>
      /// <returns></returns>
        [HttpPost]
        public ResponseModel AddBidBase(wuliu_bid_base bidbase)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);

                bidbase.account_id = accountId;
                bidbase.company_id = company.company_id;
                bidbase.company_name = company.company_name;
                bidbase.company_no = company.company_no;
                bidbase.start_bid_time = bidbase.start_bid_time / 1000;
                bidbase.end_bid_time = bidbase.end_bid_time / 1000;
                bidbase.receipt_time = bidbase.receipt_time / 1000;
                bidbase.consignee_time = bidbase.consignee_time / 1000;
                bidbase.balance = company.balance;
                bidbase.end_address2 = "";
                bidbase.end_city_name2 = "";
                bidbase.end_city_id2_x = "";
                bidbase.end_city_id2 = "";//多收货地后面需要改
                bidbase.fk = company.fk;
                bidbase.price_mode = company.price_mode;
                bidbase.bid_sel = company.bid_sel;
                bidbase.address_display = company.address_display;
                bidbase.nopickup_price = company.nopickup_price;
                bidbase.empty_price = company.empty_price;
                bidbase.bidoff_price = company.bidoff_price;
                bidbase.tax_whether = 0;//是否开启税率比较，1开启，0关闭
                bidbase.Operator = accountId.ToString();
                if (bidbase.stowage.IsNull())
                    bidbase.stowage = 1;
                bidbase.state = 1;
                bidbase.del_flag = "1";
                bidbase.bid_minnum = company.bid_minnum;
                string citybid = company.city_id_x.Split('|')[1].Substring(0, 4);
                bidbase.bid_no = NoBuilder.GetBidNo(citybid);
                bidbase.operator_time = DateTime.Now;
                var bid_id = bidService.BidBaseAdd(bidbase);

                //if(bid_id>0)
                //{
                //    string title = "【企运宝】新标单";
                //    string content = "有一条到" + bidbase.end_city_name + "新标单，请点击报价。";
                //    var list = androidService.GetCompanyidAndroidToken((int)bidbase.company_id);
                //    foreach (var mod in list)
                //    {
                //        string registrationid = mod.android_token;
                //        //PushAndroid(title, content, registrationid);//激光推送
                //        //Thread.Sleep(1000);
                       
                //    }
                //}

                //AddLog
                wuliu_bid_log log = new wuliu_bid_log();
                log.bid_id = bid_id;
                log.bid_no = bidbase.bid_no;
                log.Operator = accountId.ToString();
                log.state = bidbase.state;
                logService.AddLog(log);

                resultmod.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }
        /// <summary>
        /// 历史标单查询
        /// </summary>
        /// <param name="bidQuery"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel GetBillList(BidQuery bidQuery)
        {
            if (bidQuery == null) bidQuery = new BidQuery();
            var resultmod = new ResponseModel();
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                int company_id = companyService.GetCompanyIdByAccountId(accountId);
                var list = new List<IPredicate>();
                if (!string.IsNullOrEmpty(bidQuery.bid_no))
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.bid_no, Operator.Like, "%"+bidQuery.bid_no+"%"));
                if (!string.IsNullOrEmpty(bidQuery.ordernumber))
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.number, Operator.Like, "%"+bidQuery.ordernumber+"%"));
                if (bidQuery.state_bid_time1 > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.start_bid_time, Operator.Ge, bidQuery.state_bid_time1/1000));
                if (bidQuery.state_bid_time2 > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.start_bid_time, Operator.Le, bidQuery.state_bid_time2/1000));
                if (bidQuery.end_bid_time1 > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.end_bid_time, Operator.Gt, bidQuery.end_bid_time1/1000));
                if (bidQuery.end_bid_time2 > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.end_bid_time, Operator.Le, bidQuery.end_bid_time2/1000));


                list.Add(Predicates.Field<wuliu_bid_base>(f => f.company_id, Operator.Eq, company_id));

                if (bidQuery.del_flag > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.del_flag, Operator.Eq, bidQuery.del_flag));
                if (bidQuery.state > 0)
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Eq, bidQuery.state));
                else
                {
                    if (bidQuery.del_flag > 0)
                        list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Gt, 1));
                    else
                        list.Add(Predicates.Field<wuliu_bid_base>(f => f.state, Operator.Ge, 10));
                }


                if (!string.IsNullOrEmpty(bidQuery.wuliu_name))
                    list.Add(Predicates.Field<wuliu_bid_base>(f => f.wuliu_name, Operator.Like, "%" + bidQuery.wuliu_name + "%"));
                var pg = Predicates.Group(GroupOperator.And, list.ToArray());

                //var pre = Predicates.Field<wuliu_bid_base>(f => f.bid_no, Operator.Eq, bidQuery.bid_no);
                IPredicate pre = null;
                resultmod.mod = bidService.GetBidList(pg, bidQuery.page - 1, bidQuery.pagesize);
                resultmod.InitResultState(GetDataStateEnum.Success);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        /// <summary>
        /// 查询标单数据
        /// </summary>
        /// <param name="billid"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel BidQuery(BidQuery query)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            resultmod.mod = bidService.GetQueryBidList(query.id,query.time,query.page,query.pagesize);
            return resultmod;
        }
        /// <summary>
        /// 获取标单数据
        /// </summary>
        /// <param name="billid"></param>
        /// <returns></returns>
          [HttpPost]
        public ResponseModel GetBillMod(BidPhoneQuery bidbase)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            resultmod.mod = bidService.GetBidInfo(bidbase.bid_id);
            return resultmod;
        }
       /// <summary>
       /// 复制标单
       /// </summary>
       /// <param name="billid"></param>
       /// <returns></returns>
          [HttpPost]
          public ResponseModel CopyBillData(wuliu_bid_base bidbase)
          {
              var resultmod = new ResponseModel();
              var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
              var company = companyService.GetCompanyByAccountId(accountId);
              resultmod.InitResultState(StateEnum.Success);
              var mod = bidService.GetBid((int)bidbase.bid_id);
              mod.tax_whether = 0;//是否开启税率比较，1开启，0关闭
              mod.Operator = accountId.ToString();
              if (mod.stowage.IsNull())
                  mod.stowage = 1;
              mod.state = 1;
              mod.del_flag = "1";
              mod.bid_minnum = company.bid_minnum;
              string citybid = company.city_id_x.Split('|')[1].Substring(0, 4);
              mod.bid_no = NoBuilder.GetBidNo(citybid);
              mod.operator_time = DateTime.Now;
              resultmod.mod = bidService.BidBaseAdd(mod);
              return resultmod;
          }

        /// <summary>
        /// 更新标单管理
        /// </summary>
        /// <param name="bidbase"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseModel UpdateBidBase(wuliu_bid_base bidbase)
        {
            var resultmod = new ResponseModel();
            resultmod.InitResultState(StateEnum.Success);
            try
            {
                var accountId = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
                var company = companyService.GetCompanyByAccountId(accountId);

                bidbase.account_id = accountId;
                bidbase.company_id = company.company_id;
                bidbase.company_name = company.company_name;
                bidbase.company_no = company.company_no;
                bidbase.start_bid_time = bidbase.start_bid_time / 1000;
                bidbase.end_bid_time = bidbase.end_bid_time / 1000;
                bidbase.receipt_time = bidbase.receipt_time / 1000;
                bidbase.consignee_time = bidbase.consignee_time / 1000;

                bidbase.end_address2 = "";
                bidbase.end_city_name2 = "";
                bidbase.end_city_id2_x = "";
                bidbase.end_city_id2 = "";//多收货地后面需要改

                bidbase.fk = company.fk;
                bidbase.price_mode = company.price_mode;
                bidbase.bid_sel = company.bid_sel;
                bidbase.address_display = company.address_display;
                bidbase.nopickup_price = company.nopickup_price;
                bidbase.empty_price = company.empty_price;
                bidbase.bidoff_price = company.bidoff_price;
                bidbase.tax_whether = 0;//是否开启税率比较，1开启，0关闭
                bidbase.Operator = accountId.ToString();
                bidbase.state = 1;
                bidbase.del_flag = "1";
                bidbase.bid_minnum = company.bid_minnum;
                string citybid = company.city_id_x.Split('|')[1].Substring(0, 4);
                bidbase.bid_no = NoBuilder.GetBidNo(citybid);
                bidbase.operator_time = DateTime.Now;
                resultmod.mod = bidService.BidBaseUpdate(bidbase);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }

        [HttpPost]
        /// <summary>
        /// 删除
        /// </summary>
        public ResponseModel DeleteBid(wuliu_bid_base_query bid)
        {
            var resultmod = new ResponseModel();
            var account_id = Convert.ToInt32(HttpContext.Current.Request.Headers[LogisticsConst.LOGIN_HEADER]);
            try
            {
                int company_id = companyService.GetCompanyIdByAccountId(account_id);
                var result = bidService.DeleteBid(company_id, (int)bid.bid_id);
                //var bid = bidService.GetBid(bid_id);
                if (result == 1)
                {
                    wuliu_bid_log log = new wuliu_bid_log();
                    log.bid_id = bid.bid_id;
                    log.bid_no = bid.bid_no;
                    log.Operator = company_id.ToString();
                    log.operator_time = DateTime.Now;
                    log.state = -1;
                    resultmod.InitResultState(StateEnum.Success);
                }
                else
                    resultmod.InitResultState(StateEnum.Error);
            }
            catch (Exception ex)
            {
                resultmod.InitResultState(ex);
            }
            return resultmod;
        }



        public static void PushAndroid(string title, string content, string RegistrationID = "")
        {
            string ApiKey = "cc5c60669b05b76744a1f112";
            string APIMasterSecret = "64b111e5d59fc0c10a76a5d6";
            try
            {
                Random ran = new Random(); int sendno = ran.Next(1, 2100000000);//随机生成的一个编号   
                string app_key = ApiKey;
                string masterSecret = APIMasterSecret;
                int receiver_type = 5;
                //接收者类型。2、指定的 tag。3、指定的 alias。4、广播：对 app_key 下的所有用户推送消息。5、根据 RegistrationID 进行推送。当前只是 Android SDK r1.6.0 版本支持   
                string receiver_value = RegistrationID;
                int msg_type = 1;//1、通知2、自定义消息（只有 Android 支持）    
                string msg_content = "{\"n_builder_id\":\"00\",\"n_title\":\"" + title + "\",\"n_content\":\"" + content + "\"}";//消息内容 
                string platform = "android";//目标用户终端手机的平台类型，如： android, ios 多个请使用逗号分隔。               
                string verification_code = GetMD5Str(sendno.ToString(), receiver_type.ToString(), receiver_value, masterSecret);//验证串，用于校验发送的合法性。MD5       
                string postData = "sendno=" + sendno;
                postData += ("&app_key=" + app_key);
                postData += ("&masterSecret=" + masterSecret);
                postData += ("&receiver_type=" + receiver_type);
                postData += ("&receiver_value=" + receiver_value);
                postData += ("&msg_type=" + msg_type);
                postData += ("&msg_content=" + msg_content);
                postData += ("&platform=" + platform);
                postData += ("&verification_code=" + verification_code);
                //byte[] data = encoding.GetBytes(postData);    
                byte[] data = Encoding.UTF8.GetBytes(postData);
                string resCode = GetPostRequest(data);//调用极光的接口获取返回值      
                JpushMsg msg = Newtonsoft.Json.JsonConvert.DeserializeObject<JpushMsg>(resCode);//定义一个JpushMsg类，包含返回值信息，将返回的json格式字符串转成JpushMsg对象
            }
            catch (Exception ex) { }
        }
        /// <summary>       
        ///  /// MD5字符串      
        ///   /// </summary>       
        ///  /// <param name="paras">参数数组</param>      
        ///   /// <returns>MD5字符串</returns>      
        public static string GetMD5Str(params string[] paras)
        {
            string str = "";
            for (int i = 0; i < paras.Length; i++)
            {
                str += paras[i];
            }
            byte[] buffer = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(str));
            string md5Str = string.Empty;
            for (int i = 0; i < buffer.Length; i++)
            {
                md5Str = md5Str + buffer[i].ToString("X2");
            }
            return md5Str;
        }


        /// <summary>        
        /// /// Post方式请求获取返回值 
        ///        /// </summary>    
        /// <param name="data"></param>      
        ///   /// <returns></returns>       
        public static string GetPostRequest(byte[] data)
        {
            HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("http://api.jpush.cn:8800/v2/push"); myRequest.Method = "POST";
            //极光http请求方式为post           
            myRequest.ContentType = "application/x-www-form-urlencoded";
            //按照极光的要求        
            myRequest.ContentLength = data.Length;
            Stream newStream = myRequest.GetRequestStream();
            // Send the data.           
            newStream.Write(data, 0, data.Length);
            newStream.Close();
            // Get response           
            var response = (HttpWebResponse)myRequest.GetResponse();
            using (var reader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding("UTF-8")))
            {
                string result = reader.ReadToEnd(); reader.Close(); response.Close(); return result;
            }
        }
        public class JpushMsg
        {
            private string sendno;//编号 
            public string Sendno { get { return sendno; } set { sendno = value; } }
            private string msg_id;//信息编号      
            public string Msg_id { get { return msg_id; } set { msg_id = value; } }
            private string errcode;//返回码 
            public string Errcode { get { return errcode; } set { errcode = value; } }
            private string errmsg;//错误信息   
            public string Errmsg { get { return errmsg; } set { errmsg = value; } }
        }



        [HttpPost]
        /// <summary>
        /// 废标
        /// </summary>
        /// <param name="bid_id"></param>
        /// <returns></returns>
        public ResponseModel ScrapBid(wuliu_bid_close_query bid_close)
        {
            var responseModel = new ResponseModel();
            responseModel.InitResultState(StateEnum.Error);
            try
            {
                if (bidService.ScrapBid(bid_close))
                    responseModel.InitResultState(StateEnum.Success);
            }
            catch (Exception ex)
            {
                responseModel.InitResultState(ex);
            }
            return responseModel;
        }
    }
}